package radiostation;

import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;
import model.Artist;


public class SelectMemberForm extends javax.swing.JFrame {

    
    private EntityManager em;
    private List <Artist> members;
    private MusicGroupEditUI parent;
    
    
    
    public SelectMemberForm(EntityManager em, List<Artist> members, MusicGroupEditUI parent ) {
        this.em = em;
        this.members = members;
        this.parent = parent;
        
        initComponents();
        
        //Ρύθμιση παραθύρου να βρίσκεται στο κέντρο της οθόνης
        setLocationRelativeTo (null);
        filterArtists();
    } 
    
    public void filterArtists(){
        //Σβήνω από την λίστα ArtistList με την οποία έχει γίνει bind ο πίνακας 
        // του γραφικού περιβάλλοντος τους καλλιτέχνες του συγκροτήματος
        
        for (Artist a: members){
            artistList.remove(a);
        }
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        artistQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT a FROM Artist a");
        artistList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(artistQuery.getResultList());
        newMember = new javax.swing.JLabel();
        availableArtistsScrollPane = new javax.swing.JScrollPane();
        availableArtistsTable = new javax.swing.JTable();
        selectMemberButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Eπιλογή Νέου Μέλους");
        setAutoRequestFocus(false);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        newMember.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        newMember.setText("Επιλογή Νέου Μέλους Συγκροτήματος");

        availableArtistsTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artistList, availableArtistsTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${firstName}"));
        columnBinding.setColumnName("Όνομα");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${lastName}"));
        columnBinding.setColumnName("Επώνυμο");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${artisticName}"));
        columnBinding.setColumnName("Καλλιτεχνικό Όνομα");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        availableArtistsScrollPane.setViewportView(availableArtistsTable);

        selectMemberButton.setText("Επιλογή Νέου Μέλους");
        selectMemberButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectMemberButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Ακύρωση");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cancelButton)
                    .addComponent(availableArtistsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(39, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(newMember)
                        .addGap(103, 103, 103))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(selectMemberButton)
                        .addGap(143, 143, 143))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(newMember)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(availableArtistsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(selectMemberButton)
                .addGap(16, 16, 16)
                .addComponent(cancelButton)
                .addGap(21, 21, 21))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
       
        //Ενεργοποίηση γονικού παραθύρου
        parent.setEnabled(true);
        
        //Κλείσιμο του τρέχοντος παραθύρου
        dispose();
    }//GEN-LAST:event_formWindowClosing

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        
        //Ενεργοποίηση γονικού παραθύρου
        parent.setEnabled(true);
        
        //Κλείσιμο του τρέχοντος παραθύρου
        dispose();
      
        
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void selectMemberButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectMemberButtonActionPerformed
       
        // Έλεγχος αν έχει επιλεχθεί καλλιτέχνης
        int row = availableArtistsTable.getSelectedRow();
        
        if (row == -1){
            //Αναδυόμενο παράθυρο
            JOptionPane.showMessageDialog(this, "Δεν έχει επιλεχθεί καλλιτέχνης", "Σφάλμα", 
                            JOptionPane.WARNING_MESSAGE);
            return;
                    
        }
        
        //Ανάκτηση του επιλεγμένου καλλιτέχνη
        Artist newMember = artistList.get(row);
        
        //Εισαγωγή στην λίστα του προηγούμενου γραφικού περιβάλλοντος
        members.add(newMember);
        
        //Ενεργοποίηση του γονικού παραθύρου
        parent.setEnabled(true);
        //Κλείσιμο του τρέχοντος παραθύρου
        dispose();
        
        
        
        
        
    }//GEN-LAST:event_selectMemberButtonActionPerformed

    
    
        
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<model.Artist> artistList;
    private javax.persistence.Query artistQuery;
    private javax.swing.JScrollPane availableArtistsScrollPane;
    private javax.swing.JTable availableArtistsTable;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel newMember;
    private javax.swing.JButton selectMemberButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}